package love.contact;

import java.io.File;

import love.contact.data.Setting;
import love.contact.engine.TheApplication;
import wwk.common.util.FileUtil;
import wwk.common.util.SysUtil;
import wwk.common.widget.NavigationBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class AboutActivtiy extends BaseActivity implements OnItemClickListener {

	private ListView listView;
	private String[] stringArray;
	private ArrayAdapter<String> adapter;
	
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.common_nav_list_activity);
		
		NavigationBar nav=(NavigationBar)this.findViewById(R.id.navBar);
        nav.setTitle(getString(R.string.app_name));
        nav.setBackButton(R.layout.common_nav_back_button);
        nav.activity=this;
        
        listView=(ListView)this.findViewById(android.R.id.list);
		listView.setCacheColorHint(0);
		Drawable color=new ColorDrawable(0xFF000000);
		listView.setDivider(color);
		listView.setDividerHeight(1);
		listView.setTextFilterEnabled(true);
				
		//headView
		TextView headView=new TextView(this);
		headView.setPadding(0, 5, 0, 0);
		headView.setGravity(Gravity.CENTER_HORIZONTAL);
		headView.setTextColor(0xff000000);
		headView.setTextSize(15);
		Drawable d = getResources().getDrawable(R.drawable.qrcode);  
		DisplayMetrics dm=SysUtil.getDisplayMetrics(this);
		int width=dm.widthPixels/2;
		d.setBounds(0,0,width,width);
		headView.setCompoundDrawables(null,d,null,null); 
		listView.addHeaderView(headView);

		//version
		Setting setting=new Setting();
		setting.load(this);
		final int packageVCode=SysUtil.getVersionCode(this);
		final int serverVCode=setting.serverVersionCode;
		final String packageVName=SysUtil.getVersionName(this);
		final String serverVName=setting.serverVersionName;
		String text=getString(R.string.currentVersion)+"v"+packageVName;
		if(serverVCode>packageVCode)
		{
			text=getString(R.string.latestVersion)+"v"+serverVName+"\n"+text;
			
			/*
			//headView.setAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));  
			RotateAnimation anim=new RotateAnimation(0f, 1f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		    anim.setInterpolator(new LinearInterpolator());
		    anim.setRepeatCount(Animation.INFINITE);
		    anim.setDuration(5000);
		    headView.startAnimation(anim);
		    */
			 //闪烁
	        AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.5f, 1.0f);
	        alphaAnimation1.setDuration(500);
	        alphaAnimation1.setRepeatCount(Animation.INFINITE);
	        alphaAnimation1.setRepeatMode(Animation.REVERSE);
	        headView.setAnimation(alphaAnimation1);
	        alphaAnimation1.start();
		}
		headView.setText(text);
		
		//update
		headView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0) 
			{
				if(serverVCode>packageVCode)
				{
					TheApplication theApp=(TheApplication)getApplication();
					if(!theApp.updateController.isDownloading())
					{
						theApp.updateController.downloadNewVersion(AboutActivtiy.this);
					}
				}
			}
		});
		
		//listener
		listView.setOnItemClickListener(this);

        stringArray=new String[1];

 		adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stringArray)//stringArray锟斤拷锟斤拷锟斤拷null
 				{
		 			@Override
		 			public int getCount() 
		 			{
		 				return 3;
		 			}
		
		 			@Override
		 			public String getItem(int position) {
		 				// TODO Auto-generated method stub
		 				return ""+position;
		 			}
		
		 			@Override
		 			public long getItemId(int position) {
		 				// TODO Auto-generated method stub
		 				return position;
		 			}
		
		 			@Override
		 			public View getView(int position, View convertView, ViewGroup parent) {
		 				 if (convertView == null) 
		 		        {
		 		            convertView =  LayoutInflater.from(this.getContext()).inflate(android.R.layout.simple_list_item_1, null);
		 		        }
		 		        
		 				TextView textView=(TextView)convertView;
		 				textView.setTextColor(Color.BLACK);
		 		        int icon=0;
		 		        String text=null;
		 		        
		 		        switch(position)
		 		        { 		       
			 		      case 0:
				 		       {
				 		    		icon=R.drawable.ic_about_dark;
				 		        	text=getString(R.string.softhelp);
				 		    	   	
				 		       }
				 		       break;
			 		     case 1:
			 		       {
			 		    		icon=R.drawable.recommend;
			 		        	text=getString(R.string.recommendUs);
			 		    	   	
			 		       }
			 		       break;
			 		    case 2:
			 		       {
			 		    		icon=R.drawable.contact_us;
			 		        	text=getString(R.string.contactUs);
			 		    	   	
			 		       }
			 		       break;
		 		        }
 
		 		        textView.setText(text);
		 		        textView.setTextSize(18);
		 		       
		 		        Drawable drawable=this.getContext().getResources().getDrawable(icon);
		 		        drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
		 		        textView.setCompoundDrawables(drawable,null,null,null);
		 		        textView.setCompoundDrawablePadding(10);
		
		 		        return convertView;
		 			}
 				};
 		listView.setAdapter(adapter);
 		
    }

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
	{
		switch(position)
		{
			case 1:
				{
					startActivity(new Intent(this,IntroductionActivtiy.class));
				}
				break;
			case 2:
				{
					Intent intent=new Intent(Intent.ACTION_SEND);  
					//intent.setType("text/plain");  //分享的数据类型
					intent.setType("image/*");
					
					String sdCard=FileUtil.getSdCardDir();
			        if(sdCard!=null)
			        {
				        String path=sdCard+File.separator+SysUtil.getPackageName(this)+File.separator;
				    		if(FileUtil.isFileExists(path+"qrcode.png"))
				    		{
				    			Uri imageUri =  Uri.fromFile(new File(path+"qrcode.png"));
							intent.putExtra(Intent.EXTRA_STREAM,imageUri);//这里必须是图片的uri
				    		}
			        }

					intent.putExtra(Intent.EXTRA_SUBJECT,getString(R.string.app_name));  //主题
					intent.putExtra(Intent.EXTRA_TEXT,getString(R.string.shareContent));  //内容
					startActivity(Intent.createChooser(intent,getString(R.string.recommendILC)));  //目标应用选择对话框的标题
				}
				break;
			case 3:
				{
					String mail="progreader@sina.com";
				    Uri uri = Uri.parse("mailto:"+mail);   
				    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);  
				    intent.putExtra(Intent.EXTRA_SUBJECT,getString(R.string.feedback));
				    //intent.putExtra(Intent.EXTRA_TEXT, "body"); //body  
				    startActivity(Intent.createChooser(intent,getString(R.string.chooseOperate)));
				}
				break;
		}
	}
}